<?php
if(!isset($_GET['photo_id']))
{
	exit;
}

if(!function_exists('displayName'))
{
	function displayName($value)
	{
		return str_replace('.', ' ', $value);
	}
}

include_once($_SERVER['DOCUMENT_ROOT'].'/php/class/db/mysql.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/php/class/photo.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/php/libs/ajaxstarrater/_drawrating.php');

$photo = new JVPhoto($_GET['photo_id']);
$photo_data = $photo->restore();
if(!$photo_data)
{
	exit;
}

echo '<img src="' . $photo->resize(250, 250) . '" />';

echo rating_bar($_GET['photo_id'],5);
?>

<p class="icon download">
	<a href="/photos.php?telecharger&photo_id=<?php echo $_GET['photo_id']; ?>">T&eacute;l&eacute;charger</a>
</p>
<p class="icon rotate">
	<a href="/photos.php?rotation&photo_id=<?php echo $_GET['photo_id']; ?><?php echo (isset($_GET['folder'])?'&folder='.$_GET['folder']:''); ?>">Rotation de -90 degr&eacute;</a>
</p>
<p class="icon rotate_back">
	<a href="/photos.php?rotation&back&photo_id=<?php echo $_GET['photo_id']; ?><?php echo (isset($_GET['folder'])?'&folder='.$_GET['folder']:''); ?>">Rotation de +90 degr&eacute;</a>
</p>
<p>
	<form id="form_name_photo" action="/photos.php?photo_id=<?php echo $_GET['photo_id']; ?><?php echo (isset($_GET['folder'])?'&folder='.$_GET['folder']:''); ?>" method="POST">
	<label for="change_name"><strong>titre</strong> </label>
	<input id="name" name="change_name" value="<?php echo displayName($photo_data['NAME']); ?>" class="validate['required']"/>
	<input type="submit" value="ok" />
	</form>

	<script type="text/javascript">
        new FormCheck('form_name_photo', {
        	display : {
                titlesInsteadNames : true
            }
        });
	</script>
	
	<form action="/photos.php?photo_id=<?php echo $_GET['photo_id']; ?>" method="POST" id="form_folder_photo">
	<label for="change_folder"><strong>dossier</strong> </label>
	<select name="change_folder" id="select_change_folder">
		<?php
			$db = JVMySQL::get();
			$result = $db->query('SELECT FOLDER FROM PHOTOS GROUP BY FOLDER');
			while ($row = $db->row($result))
			{
				$folderName = displayName($row['FOLDER']);
				if(strlen($folderName) > 20)
				{
					$folderName = substr($folderName, 0, 20) . '...';
				}
				
				echo '<option value="'.$row['FOLDER'].'"'.($row['FOLDER'] == $photo_data['FOLDER']? ' selected="selected"':'' ).'>
					'.$folderName.'
				</option>';
			}
			echo '<option value="Nouveau">Nouveau dossier ...</option>';
			
		?>
	</select>
	<input name="change_folder" id="input_change_folder" disabled="disabled" style="display:none;" class="validate['required']" />
	<input type="submit" value="ok" />
	</form>
	
	<script type="text/javascript">
		function newFolderCreation()
		{
			$('select_change_folder').setStyle('display', 'none');
			$('input_change_folder').setStyle('display', '');
			$('input_change_folder').setProperty('disabled', '');
		}
		
		function selectFolderChange()
		{
			if($('select_change_folder').options[$('select_change_folder').selectedIndex].value == 'Nouveau')
			{
				newFolderCreation();
			}
		}
		
		$('select_change_folder').addEvent('change', selectFolderChange);
		selectFolderChange();
		
		 new FormCheck('form_folder_photo', {
        	display : {
                titlesInsteadNames : true
            }
        });
	</script>
</p>

<p>
	<form id="form_message_photo" action="/photos.php?message&photo_id=<?php echo $_GET['photo_id']; ?><?php echo (isset($_GET['folder'])?'&folder='.$_GET['folder']:''); ?>" method="POST">
		<table width="100%">
		<tbody>
		  <tr>
		    <td align="center">
			Pr&eacute;nom :<br /> 
			<input id="prenom" type="text" name="name_photo" class="icon validate['required']" />
			</td>
		  </tr>
		  <tr>
		    <td align="center">
			Message :<br /> 
			<textarea name="message_photo" rows="6" cols="25" class="validate['required']"></textarea>
			</td>
		  </tr>
		  <tr>
		    <td align="center">
			<input type="submit" value="Envoyer" />
			</td>
		  </tr>
		</tbody>
		</table>
	</form>
	
	<script type="text/javascript">
        new FormCheck('form_message_photo', {
        	display : {
                titlesInsteadNames : true
            }
        });
	</script>
			
	<?php
	/*Ajout d'un post dans la base de donnee*/
	$db = JVMySQL::get();
	if (isset($_POST['message_photo']) && isset($message_photo_ok)) 
	{
		echo "<p class=\"icon info\">Votre message &agrave; bien &eacute;t&eacute; enregistr&eacute;.</p>";
	}
	?>
	
	<table width="100%">
		<colgroup>
			<col width="100%" />
		</colgroup>
		<tbody>
		<?php
			$result = $db->query("SELECT * FROM `COMMENTS` WHERE PHOTO_ID = ".$_GET['photo_id']." ORDER BY DATE DESC");
			while ($data = $db->row($result))
			{ 
			?>
			  <tr>
	   		    <td>
					<?php
					$nom_mois = array(  "Janvier",
								  	    "F&eacute;vrier",
								  	    "Mars",
										"Avril",
										"Mai",
										"Juin",
										"Juillet",
										"Ao&ucirc;t",
										"Septembre",
										"Octobre",
										"Novembre",
										"D&eacute;cembre");
					
					$msg_day = strtotime($data['DATE']);
					$thead_msg  = "<img class=\"comments\" src=\"/images/site/photos/comment.png\" /><em>le ";
					$thead_msg .= date("d", $msg_day).' ';
					$thead_msg .= $nom_mois[ date("m", $msg_day) - 1].' ';
					$thead_msg .= date("Y", $msg_day);
					$thead_msg .= "</em>, ";
					$thead_msg .= "<strong>".(empty($data['NAME']) ? 'invit&eacute;' : $data['NAME'])."</strong> a dit :";
					echo $thead_msg;
					?>
			    </td>
			  </tr>
			  <tr>
			    <td class="comment"><?php echo nl2br(stripslashes($data['SAY']));?></td>
			  </tr>
			<?php 
			}
			?>
		</tbody>
	</table>
</p>
